package com.wanshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanshi.entity.Cust;
import com.wanshi.entity.Result;
import com.wanshi.service.ICustService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-08-05
 */
@RestController
@RequestMapping("/cust")
@CrossOrigin
public class CustController {


    @Autowired
    private ICustService custService;




    //1 带条件的分页查询
    @PostMapping("/search/{current}/{size}")
    public Page<Cust> search(@PathVariable int current,@PathVariable int size,
                             @RequestBody Cust cust){

        QueryWrapper<Cust> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(cust.getUsername())){
            wrapper.like("username",cust.getUsername());
        }
        if(StringUtils.isNotBlank(cust.getGender())){
            wrapper.eq("gender",cust.getGender());
        }
        if(StringUtils.isNotBlank(cust.getType())){
            wrapper.eq("type",cust.getType());
        }

        wrapper.orderByDesc("create_time");

        Page<Cust> page = new Page(current,size);
        custService.page(page,wrapper);
        return page;
    }

    //根据id查询客户详情
    @GetMapping("/{id}")
    public Cust findById(@PathVariable String id){
        return custService.getById(id);
    }

    //添加 或 修改
    @PostMapping
    public Result addOrUpdate(@RequestBody Cust cust){
        boolean flag = custService.saveOrUpdate(cust);
        if(flag){
            return new Result(200,"成功");
        }else{
            return new Result(1001,"添加或更新失敗");
        }
    }

    //删除
    //添加
    @DeleteMapping("/{id}")
    public Result del(@PathVariable String id){
        boolean flag = custService.removeById(id);
        if(flag){
            return new Result(200);
        }else{
            return new Result(1001,"刪除失敗");
        }
    }

}
